/**
 * 
 */
package validator;

import java.io.IOException;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.util.ArrayList;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

import org.apache.commons.lang3.StringUtils;

/**
 * @author Yokesh Kumar
 *
 */
public class Validator {

	/**
	 * This method checks whether the phoneNumber contains only numbers
	 * @param PhoneNumber
	 * @return
	 */
	public static boolean validateStringContainsInteger(String value){

		try{
			for(int i=0;i<value.length();i++){
				Integer.parseInt(String.valueOf(value.charAt(i)));
			}
			return true;
		}
		catch(NumberFormatException exec){
			return false;
		}		
	}

	
public static Stage dialogstage;
	
	/**
	 * Dialog Box to prompt the user alert.
	 */
	public void dialogBox()
	{
		Stage dialogStage = new Stage();
		dialogStage.initModality(Modality.WINDOW_MODAL);
		AnchorPane diaAnchorPane=null;
		 FXMLLoader myLoader = new  FXMLLoader(getClass().getResource("/rentReturnUI/dialog.fxml"));
		 try {
			 diaAnchorPane = (AnchorPane) myLoader.load();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 dialogStage.setTitle("Confirmation Box");
		dialogStage.setScene(new Scene(diaAnchorPane));
		this.dialogstage=dialogStage;
		dialogStage.show();
	}

	public static boolean validatePhoneNumber(String value)
	{
		if(value.length()==10)
			if(validateStringContainsInteger(value))
		return true;
		
		return false;
	}
	
	public void closeDialog()
	{
		dialogstage.close();
	}
	public static boolean validateStringContainsDouble(String value){

		if(value.contains("-"))
			return false;
		try{
			Double.parseDouble(value);
		}
		catch(NumberFormatException exec){
			return false;
		}

		return true;
	}
	/**
	 * This method checks whether the emailId is valid
	 * @param emailId
	 * @return
	 */
	public static boolean validateEmailID(String emailId){
		String email_regex = "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
			    
			      Boolean b = emailId.matches(email_regex);
			      return b;
	}

	/**
	 * This method checks whether the name contains only alphabets
	 * @param name
	 * @return
	 */
	public static boolean validateName(String value){

		String nameArray[] = value.split("\\s");
		for(int i=0;i<nameArray.length;i++){
			if(!StringUtils.isAlpha(nameArray[i])){
				return false;
			}

		}
		return true;
	}


	public static void addTextLimiter(final TextField tf, final int maxLength) {
		tf.textProperty().addListener(new ChangeListener<String>() {
			@Override
			public void changed(final ObservableValue<? extends String> ov, final String oldValue, final String newValue) {
				if(tf != null && !StringUtils.isEmpty(tf.getText())){
					if (tf.getText().length() > maxLength) {
						String s = tf.getText().substring(0, maxLength);
						tf.setText(s);
					}
				}
			}
		});
	}

	/**
	 * This method checks if both the pwds are same
	 * if yes, returns true
	 * @param pwd
	 * @param confirmPwd
	 * @return
	 */
	public static boolean verifyPwd(String pwd, String confirmPwd){
		if(pwd.equals(confirmPwd)){
			return true;
		}
		else{
			return false;
		}

	}

	public static boolean validateLength(String value, int maxLength){
		if(value.length() > maxLength){
			return false;
		}
		else{
			return true;
		}
	}

	public static boolean validateFutureDate(LocalDate date)
	{
		if (date.isAfter(LocalDate.now()) || date.isEqual(LocalDate.now()))
		{
			return true;
		}
		return false;
	}

	public static ArrayList<String> timeRange()
	{   String time=null;
	ArrayList<String> range=new ArrayList<String>();

	for(int i=18;i<=38;i++)
	{
		if(i%2==0)
			time= (i/2) +":"+"00";
		else
			time= (i/2) +":"+"30";
		range.add(time);
	}
	return range;
	}

	
	
	public static double formatDouble(double value){
		
		DecimalFormat format = new DecimalFormat("#.00");
		String d = format.format(value);
		return new Double(d);
	}

	public static double formatDoubleWithRound(double value){
		DecimalFormat format = new DecimalFormat("#.00");
		String d = format.format(value);
		int last = Integer.parseInt(d.substring(d.length()-1));
		int lastTwo = Integer.parseInt(d.substring(d.length()-2, d.length()-1));
		System.out.println(d);
		int splitPoints= d.indexOf(".");
		int firstHalf=Integer.parseInt(d.substring(0, splitPoints));
		//int beforePoint = Integer.parseInt(splitPoints[0]);
		System.out.println(last+"    "+lastTwo+"   "+splitPoints+"   "+firstHalf);
		
		if(last>0 && last <=5)
			last=5;
		else if(last >5 && last <=9)
		{
			last=0;
			lastTwo++;
			if(lastTwo==10)
				{  lastTwo=0;
				   firstHalf++;
				}
		}
		double finalRound = Double.parseDouble(""+firstHalf+"."+lastTwo+last);
		String round = format.format(finalRound);
		System.out.println("Changed number is "+ round);
		
		return new Double(round);
	}
}
